package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.api.MessageModel;
import in.muton.edushare.components.messaging.util.MessageTypeParser;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.panels.paginator.EdusharePageNavigator;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.MessageComments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class IncomingMessagesPanel extends Panel {

	public IncomingMessagesPanel(String id, Departments dept) {
		super(id);
		model = new MessageModel(dept, EduShareSession.get().getLoggedInUser());
		init(id);
	}

	public IncomingMessagesPanel(String id, Universities univ) {
		super(id);
		model = new MessageModel(univ, EduShareSession.get().getLoggedInUser());
		init(id);
	}

	public IncomingMessagesPanel(String id, CourseInstances cInst) {
		super(id);
		model = new MessageModel(cInst, EduShareSession.get().getLoggedInUser());
		init(id);
	}

	public IncomingMessagesPanel(String id, Users pageOwner) {
		super(id);
		model = new MessageModel(pageOwner, EduShareSession.get().getLoggedInUser());
		init(id);
	}
	
	public IncomingMessagesPanel(String id, Groups pageOwner) {
		super(id);
		model = new MessageModel(pageOwner, EduShareSession.get().getLoggedInUser());
		init(id);
	}

	private void init(String id) {
		add(CSSPackageResource.getHeaderContribution(
				IncomingMessagesPanel.class, "IncomingMessages.css"));
		add(JavascriptPackageResource.getHeaderContribution(
				IncomingMessagesPanel.class, "IncomingMessages.js"));
		PageableListView<IModel<Messages>> plv;
		add(plv = new PageableListView<IModel<Messages>>("messageList", model,ApplicationConstants.PAGE_SIZE) 
		{
			@Override
			protected void populateItem(ListItem<IModel<Messages>> arg0) {
				final boolean visible = MessageTypeParser.isVisible(arg0.getModelObject());
				arg0.setVisible(visible);
				arg0.add(MessageTypeParser.getAppropriatePanel(arg0.getModelObject()));
				arg0.add(new CommentsPanel("resp", arg0.getModelObject()));
				arg0.add(new ListView<IModel<MessageComments>>("cmtList",
						new CayenneList<MessageComments>(arg0.getModelObject()
								.getObject().getComments())) {
					@Override
					protected void populateItem(
							ListItem<IModel<MessageComments>> arg0) {
						arg0.add(new CommentDisplay("cmt", arg0
								.getModelObject()));

					}

					private static final long serialVersionUID = 806246408670543607L;
				});
			}

			private static final long serialVersionUID = 8280460025695729928L;
		});

		add(new EdusharePageNavigator("pagingControl", plv));
	}

	private MessageModel model = null;
	private static final long serialVersionUID = -4809580388254235930L;
}
